package cn.dengta.webapp.base.entity;

import java.util.*;
import java.util.stream.Collectors;

import cn.dengta.common.model.ValueProxy;
import cn.dengta.common.model.ValueProxyImpl;
import cn.dengta.common.web.Client;
import lombok.Getter;
import me.codeplayer.util.CollectionUtil;
import me.codeplayer.util.X;

@Getter
public enum ChannelPackageName implements ValueProxyImpl<ChannelPackageName, String> {

	/** main */
	main("main", ChannelName.main.getValue(), ChannelName.main.androidPackage, Client.APP_ANDROID, ChannelAppType.AD),
	/** ios_main */
	ios_main("iosMain", ChannelName.main.getValue(), ChannelName.main.iosPackage, Client.APP_IOS, ChannelAppType.AD),

	//
	;

	// 定义私有变量
	public final String name;
	public final String alias;
	public final Client client;
	public final ChannelAppType appType;
	final ValueProxy<ChannelPackageName, String> proxy;

	// 构造函数，枚举类型只能为私有
	ChannelPackageName(String label, String alias, String name, Client client, ChannelAppType appType) {
		this.name = name;
		this.alias = alias;
		this.client = client;
		this.appType = appType;
		this.proxy = new ValueProxy<>(this, name, label);
	}

	public static List<String> findChannelAlias(ChannelAppType appType) {
		return find().get(appType).stream().map(ChannelPackageName::getAlias).distinct().collect(Collectors.toList());
	}

	public static ChannelPackageName[] getValues() {
		return ValueProxy.getCachedArray(ChannelPackageName.class, ChannelPackageName::values);
	}

	public static ChannelPackageName of(ChannelAppType appType, String value) {
		return CollectionUtil.findFirst(find().get(appType), t -> t.getValue().equals(value));
	}

	public static ChannelPackageName ofOrDefault(ChannelAppType appType, String value, Client client) {
		ChannelPackageName channel = of(appType, value);
		return channel == null ? findDefault(appType, client) : channel;
	}

	private static ChannelPackageName findDefault(ChannelAppType appType, Client client) {
		switch (appType) {
			case AD:
				return ios_main.client == client ? ios_main : main;
			case AF:
				return null;
			case INTER_AD:
				return null;
			default:
				throw new UnsupportedOperationException();
		}
	}

	public static Map<ChannelAppType, List<ChannelPackageName>> APP_TYPE_CHANNEL_NAME;

	public static Map<ChannelAppType, List<ChannelPackageName>> find() {
		Map<ChannelAppType, List<ChannelPackageName>> typeChannelName = APP_TYPE_CHANNEL_NAME;
		if (typeChannelName == null) {
			final ChannelAppType[] values = ChannelAppType.getValues();
			typeChannelName = CollectionUtil.newHashMap(X.size(values));
			final ChannelPackageName[] packageNames = getValues();
			for (ChannelAppType value : values) {
				typeChannelName.put(value, Arrays.stream(packageNames).filter(c -> c.appType == value).collect(Collectors.toList()));
			}
			APP_TYPE_CHANNEL_NAME = typeChannelName;
		}
		return typeChannelName;
	}

	public static ChannelPackageName ofAlias(ChannelAppType appType, String alias, Client client) {
		return X.getElse(CollectionUtil.findFirst(find().get(appType), t -> t.alias.equalsIgnoreCase(alias) && t.client == client), () -> findDefault(appType, client));
	}

}
